﻿#region 文件信息

/*----------------------------------------------------------------
//
// 文件名称：
// 文件功能描述：
// 设计要求：
//
// 文 件 名：    FlowDesign
// 创建者：      杨程
// 创建日期：	    2022/12/18 15:07:08

//----------------------------------------------------------------*/

#endregion

namespace Vampirewal.Core.WPF.Theme;

/// <summary>
/// 流程设计
/// </summary>
public partial class FlowDesign : UserControl
{
    static FlowDesign()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(FlowDesign), new FrameworkPropertyMetadata(typeof(FlowDesign)));
    }

    /// <summary>
    ///
    /// </summary>
    public FlowDesign()
    {
        //构造函数
    }

    private Canvas canvas { get; set; }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        var StartNode = this.Template.FindName("StartNode", this) as Border;
        var ToDoNode = this.Template.FindName("ToDoNode", this) as Border;
        var EndNode = this.Template.FindName("EndNode", this) as Border;
        canvas = this.Template.FindName("canvas", this) as Canvas;

        StartNode.MouseLeftButtonDown += StartNode_MouseLeftButtonDown;
        ToDoNode.MouseLeftButtonDown += StartNode_MouseLeftButtonDown;
        EndNode.MouseLeftButtonDown += StartNode_MouseLeftButtonDown;

        canvas.Drop += Canvas_Drop;

        canvas.SizeChanged += Canvas_SizeChanged;
    }

    private void Canvas_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        Canvas canvas = (Canvas)sender;
        //canvas.Width = e.;
        //canvas.Height = canvas.ActualHeight;
    }

    private void Canvas_Drop(object sender, DragEventArgs e)
    {
        Canvas canvas = (Canvas)sender;

        var data = e.Data.GetData(typeof(string));

        FlowNode node = new FlowNode();
        node.Cursor = System.Windows.Input.Cursors.Hand;
        if (data.ToString() == "开始")
        {
            node.NodeType = 1;
        }
        else if (data.ToString() == "执行中")
        {
            node.NodeType = 2;
        }
        else if (data.ToString() == "结束")
        {
            node.NodeType = 99;
        }

        node.NodeName = data.ToString();

        node.DragDelta += Node_DragDelta;
        node.DragStarted += Node_DragStarted;

        canvas.Children.Add(node);

        var point = e.GetPosition(canvas);

        Canvas.SetLeft(node, point.X);
        Canvas.SetTop(node, point.Y);
    }

    private void Node_DragStarted(object sender, DragStartedEventArgs e)
    {
    }

    private void Node_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
    {
        FlowNode myThumb = (FlowNode)sender;
        double nTop = Canvas.GetTop(myThumb) + e.VerticalChange;
        double nLeft = Canvas.GetLeft(myThumb) + e.HorizontalChange;

        if (nTop < 0)
        {
            nTop = 0;
        }
        else if ((nTop + myThumb.Height) > canvas.Height)
        {
            nTop = canvas.Height - myThumb.Height;
        }

        if (nLeft < 0)
        {
            nLeft = 0;
        }
        else if ((nLeft + myThumb.Width) > canvas.Height)
        {
            nLeft = canvas.Height - myThumb.Width;
        }

        //myThumb.

        Canvas.SetTop(myThumb, nTop);
        Canvas.SetLeft(myThumb, nLeft);
        myThumb.ToolTip = $"X:{nTop}\r\nY:{nLeft}";
    }

    private List<string> NodeName = new List<string>();

    private void StartNode_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        Border border = (Border)sender;

        var nodename = border.Tag.ToString();

        if (NodeName.Any(a => a.Equals(nodename)) && (nodename == "开始" || nodename == "结束"))
        {
            return;
        }

        NodeName.Add(nodename);

        DragDrop.DoDragDrop(border, border.Tag, DragDropEffects.Copy);
    }

    #region [     依赖属性     ]

    public IEnumerable FlowNodes
    {
        get { return (IEnumerable)GetValue(FlowNodesProperty); }
        set { SetValue(FlowNodesProperty, value); }
    }

    // Using a DependencyProperty as the backing store for FlowNodes.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FlowNodesProperty =
        DependencyProperty.Register("FlowNodes", typeof(IEnumerable), typeof(FlowDesign), new PropertyMetadata(null));

    public IEnumerable FlowUnits
    {
        get { return (IEnumerable)GetValue(FlowUnitsProperty); }
        set { SetValue(FlowUnitsProperty, value); }
    }

    // Using a DependencyProperty as the backing store for FlowUnits.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FlowUnitsProperty =
        DependencyProperty.Register("FlowUnits", typeof(IEnumerable), typeof(FlowDesign), new PropertyMetadata(null));

    #endregion
}